/*
 * @Description: 元数据上下文
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2022-09-08 14:42:55
 */

import { createContext, useContext, useEffect, useReducer } from 'react';
import {
  EDIT_BUSINESS,
  INIT_QUERY,
  METADATA_LOADING,
  METADATA_LOADING_FINISH,
  SET_METADATA_LIST,
  SET_SELECT_AT,
  SET_SELECT_FOND,
  SET_SELECT_LEVEL
} from '../constants';
import { MetadataBusinessReducer } from '../reducers/MetadataBusinessReducer';
import { editMetadataBusinessById, fetchMetadata } from '@/api/metadata';
import { QUERY_METADATA_ALL } from '@/view/system/Metadata/constants';

const initialState = {
  // 当前选中的项目
  selectFond: {},
  selectAT: {},
  selectLevel: {},

  metadataLoading: false,
  metadataList: [],
  listQuery: {},

  // dictionarys
  duplicateList: [
    { id: 0, name: '允许', color: 'blue' },
    { id: 1, name: '不允许', color: 'red' }
  ],
  nullList: [
    { id: 0, name: '允许', color: 'blue' },
    { id: 1, name: '不允许', color: 'red' }
  ],
  deleteList: [
    { id: 0, name: '正常', color: 'blue' },
    { id: 1, name: '删除', color: 'red' }
  ],
  typeList: [
    { id: 0, name: '字符型' },
    { id: 1, name: '整型' },
    { id: 2, name: '小数型' },
    { id: 3, name: '日期型' },
    { id: 4, name: '时间型' },
    { id: 5, name: '大对象' }
  ],
  kindList: [
    { id: 0, name: '系统字段', color: 'blue' },
    { id: 1, name: '业务字段', color: 'green' }
  ]
};

const MetadataBusinessContext = createContext();

const MetadataBusinessContextProvider = (props) => {
  const [state, dispatch] = useReducer(MetadataBusinessReducer, initialState);

  const actions = {
    /**
     * @description: 设置选中的全宗
     */
    setSelectFond(data) {
      dispatch({ type: SET_SELECT_FOND, data });
    },

    /**
     * @description: 设置选中的档案类型
     */
    setSelectAT(data) {
      dispatch({ type: SET_SELECT_AT, data });
    },

    /**
     * @description: 设置选中的层级
     */
    setSelectLevel(data) {
      dispatch({ type: SET_SELECT_LEVEL, data });
    },

    /**
     * @description: 请求级别元数据列表方法
     * @param {object} fieldQuery 请求参数
     * @return {*}
     */
    fetchMatedataFunc(coordinate) {
      dispatch({ type: METADATA_LOADING });
      const fieldQuery = {
        metadataFieldType: QUERY_METADATA_ALL,
        coordinate
      };
      fetchMetadata(fieldQuery)
        .then((res) => {
          const { data } = res;
          dispatch({ type: SET_METADATA_LIST, data });
        })
        .catch((err) => {
          console.log(err);
          dispatch({ type: SET_METADATA_LIST, data: [] });
        })
        .finally(() => {
          dispatch({ type: METADATA_LOADING_FINISH });
        });
    },

    /**
     * @description: 初始化排序列表请求条件
     * @param {object} data 修改的请求内容
     */
    initListQuery(data) {
      dispatch({ type: INIT_QUERY, data });
    },

    editBusinessFunc(query) {
      editMetadataBusinessById(query).then((res) => {
        const { data } = res;
        dispatch({ type: EDIT_BUSINESS, data });
      });
    }
  };

  // watch listQuery来加载列表
  useEffect(() => {
    if (state.listQuery && JSON.stringify(state.listQuery) !== '{}') {
      actions.fetchMatedataFunc(state.listQuery);
    }
  }, [state.listQuery]);

  return (
    <MetadataBusinessContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </MetadataBusinessContext.Provider>
  );
};

export const useMetadataBusinessContext = () => {
  return useContext(MetadataBusinessContext);
};

export { MetadataBusinessContext, MetadataBusinessContextProvider };
